package com.yungu.swift.system.finance.service.impl;


import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.api.ApiPageParam;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.finance.dao.FinanceOrderMapper;
import com.yungu.swift.system.finance.model.dto.FinanceOrderDto;
import com.yungu.swift.system.finance.model.param.AdminOrderFlowPageParam;
import com.yungu.swift.system.finance.model.param.AdminOrderPayParam;
import com.yungu.swift.system.finance.model.vo.FinanceOrderVo;
import com.yungu.swift.system.finance.model.vo.OrderPayDetailVo;
import com.yungu.swift.system.finance.service.FinanceOrderService;
import com.yungu.swift.system.finance.model.enums.FinanceEnum;
import com.yungu.swift.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class FinanceOrderServiceImpl extends BaseServiceImpl<FinanceOrderDto>
        implements FinanceOrderService {
    @Autowired
    private FinanceOrderMapper financeOrderMapper;

    @Override
    protected IMapper<FinanceOrderDto> getMapper() {
        return financeOrderMapper;
    }

    @Override
    public ResponseData<PageVo<FinanceOrderVo>> queryPageVo(AdminOrderFlowPageParam flowPageParam) {
        PageBounds pageBounds = new PageBounds(flowPageParam.getPageNum(), flowPageParam.getPageSize());
        PageList<FinanceOrderVo> pageList = financeOrderMapper.queryPageVo(MapUtils.convertObjToMap(flowPageParam), pageBounds);
        fillDate(pageList);
        PageVo<FinanceOrderVo> pageVo = new PageVo(flowPageParam.getPageNum(), flowPageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<List<FinanceOrderVo>> queryPageVoExport(AdminOrderFlowPageParam flowPageParam) {
        List<FinanceOrderVo> financeOrderVos = financeOrderMapper.queryPageVo(MapUtils.convertObjToMap(flowPageParam));
        fillDate(financeOrderVos);
        return ResponseData.buildSuccessResponse(financeOrderVos);
    }

    /**
     * 补充数据含义
     *
     * @param list
     */
    private void fillDate(List<FinanceOrderVo> list) {
        list.stream().forEach(financeOrderVo -> financeOrderVo.setCourseDescription(FinanceEnum.getMsg(financeOrderVo
                .getCourse()))
        );
    }

    @Override
    public ResponseData<FinanceOrderVo> countPayOrder(AdminOrderFlowPageParam flowPageParam) {
        FinanceOrderVo paidOrder = financeOrderMapper.findPaidOrder(MapUtils.convertObjToMap(flowPageParam));
        FinanceOrderVo unpaidOrder = financeOrderMapper.findUnpaidOrder(MapUtils.convertObjToMap(flowPageParam));
        paidOrder.setUnpaidOrder(unpaidOrder.getUnpaidOrder());
        paidOrder.setUnpaidFare(unpaidOrder.getUnpaidFare());
        return ResponseData.buildSuccessResponse(paidOrder);
    }

    @Override
    public ResponseData<BigDecimal> queryTodayTotalIncome4Driver(String driverUuid) {
        BigDecimal income = financeOrderMapper.queryTodayTotalIncome4Driver(driverUuid);
//        return ResponseData.buildSuccessResponse(income.setScale(1, BigDecimal.ROUND_HALF_UP));
        return ResponseData.buildSuccessResponse(income.setScale(2, BigDecimal.ROUND_HALF_UP));
    }

    @Override
    public ResponseData<BigDecimal> findIncomeByTime(Map<String, Object> map) {
        BigDecimal income = financeOrderMapper.findIncomeByTime(map);
//        return ResponseData.buildSuccessResponse(income.setScale(1, BigDecimal.ROUND_HALF_UP));
        return ResponseData.buildSuccessResponse(income.setScale(2, BigDecimal.ROUND_HALF_UP));
    }

    @Override
    public ResponseData<List<FinanceOrderDto>> findByThawDay(Integer thawDay) {
        return ResponseData.buildSuccessResponse(financeOrderMapper.findByThawDay(thawDay));
    }

    @Override
    public ResponseData<Map<String, Object>> driverBill(Map<String, Object> conditions, ApiPageParam pageParam) {
        BigDecimal totalMoney = financeOrderMapper.getDriverTotalMoney(conditions);
        //明细列表
        PageList pageList = financeOrderMapper.driverBill(conditions, new PageBounds(pageParam.getNowPage(),
                pageParam.getPageSize()));
        // 返还结果
        Map<String, Object> result = new HashMap<>(2);
        result.put("total", totalMoney);
        result.put("list", pageList);
        return ResponseData.buildSuccessResponse(result);
    }

    /**
     * 订单支付统计
     * @param adminOrderPayParam
     * @return
     */
    @Override
    public ResponseData<PageVo<OrderPayDetailVo>> payDetail(AdminOrderPayParam adminOrderPayParam) {
        PageBounds pageBounds = new PageBounds(adminOrderPayParam.getPageNum(), adminOrderPayParam.getPageSize());
        PageList<OrderPayDetailVo> pageList = financeOrderMapper.payDetail(MapUtils.convertObjToMap(adminOrderPayParam),pageBounds);
        PageVo<OrderPayDetailVo> pageVo = new PageVo(adminOrderPayParam.getPageNum(), adminOrderPayParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    /**
     * 订单支付统计
     * @param adminOrderPayParam
     * @return
     */
    @Override
    public ResponseData<List<OrderPayDetailVo>> exportPayDetail(AdminOrderPayParam adminOrderPayParam) {
        List<OrderPayDetailVo> list = financeOrderMapper.exportPayDetail(MapUtils.convertObjToMap(adminOrderPayParam));
        return ResponseData.buildSuccessResponse(list);
    }
}
